// 加载精选整合包
// TODO:异常
$.post(path + "/IntegrationPackageServlet", {
    type: "getSelectedIntegrationPackages",
}, function (data) {
    // 获取精选整合包区域
    const featured = $("#featured-integration-package");
    const header = featured.find(">.featured-integration-package-container > .featured-integration-package-header");

    // 创建整合包卡片盒子
    const cards = $("<div class='featured-integration-package-cards'></div>")

    header.after(cards);
    $.each(data, function (index, item) {
        const card = $(`
            <div class='featured-integration-package-card'>
                <div class="featured-integration-package-card-img">
                    <img alt="" src="">
                </div>
                <div class="featured-integration-package-card-content">
                    <div class="featured-integration-package-card-title">
                        <!-- 获取名称 -->
                        <h3>${item.name}</h3>
                        <div class="featured-integration-package-card-likeCount">
                            <i class="fa fa-thumbs-o-up"></i>
                            <!-- 获取点赞量 -->
                            <span></span>
                        </div>
                    </div>
                    <!-- 获取描述 -->
                    <p class="featured-integration-package-card-desc">${item.introductionInfo}</p>
                    <div class="featured-integration-package-card-tags">
                        <!-- 获取主元素 -->
                        <span class="main-element featured-integration-package-card-tag">${item.mainElementName}</span>
                    </div>
                    <div class="featured-integration-package-card-footer">
                        <a class="featured-integration-package-card-btn" href="#">查看详情</a>
                    </div>
                </div>
            </div>`);
        card.find(".featured-integration-package-card-img img").attr("src", path + "/img/" + item.cover);

        $.post(path + "/IntegrationPackageServlet", {
            type: "getElement",
            integrationPackageId: item.id
        }, function (data) {
            $.each(data, function (index, item) {
                card.find(".featured-integration-package-card-tags").append($(`<span class="featured-integration-package-card-tag">${item.name}</span>`));
            });
        }, "json").fail(function (jqXHR, textStatus, errorThrown) {
            console.error("获取整合包元素失败:", textStatus, errorThrown);
        });

        // 格式化点赞数
        card.find(".featured-integration-package-card-likeCount span").text(formatNumberWithUnit(item.likeCount));

        cards.append(card);

        // 移除加载提示
        $(".featured-integration-package-loading").remove()
    });
}, "json").fail(function (jqXHR, textStatus, errorThrown) {
    console.error("获取精选整合包失败:", textStatus, errorThrown);
});